package com.railway.switchs.action.business;

import com.railway.switchs.aspect.OperationLog;
import com.railway.switchs.base.BaseResponse;
import com.railway.switchs.constant.SystemConstant;
import com.railway.switchs.domain.dto.business.BaseInfo;
import com.railway.switchs.domain.query.business.BaseInfoQuery;
import com.railway.switchs.enums.EnumInfoType;
import com.railway.switchs.enums.EnumReturnResult;
import com.railway.switchs.service.business.IBaseInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @ClassName BaseInfoAction
 * @Author 魏梓晟
 * @Version 1.0
 **/
@Controller
@Slf4j
@RequestMapping("/back/baseInfo")
public class BaseInfoAction {

    @Autowired
    private IBaseInfoService baseInfoService;

    /**
     * 根据类型进入基础信息页面
     * @param infoType
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(String infoType){
        if(EnumInfoType.INFO_BUREAU.getCode().equals(infoType)){
            return "baseInfo/bureau/list";
        }
        if(EnumInfoType.INFO_LINE.getCode().equals(infoType)){
            return "baseInfo/line/list";
        }
        if(EnumInfoType.INFO_STATION.getCode().equals(infoType)){
            return "baseInfo/station/list";
        }
        log.error("传入的基础信息类型有误");
        return "system/error";
    }

    /**
     * 获取铁路局信息数据
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getBureauList",method = RequestMethod.POST)
    public BaseInfoQuery getBureauList(BaseInfoQuery query) {
        try {
            query.setInfoType(EnumInfoType.INFO_BUREAU.getCode());
            query = baseInfoService.query(query);
            query.setCode(EnumReturnResult.RETURN_200.getCode());
        } catch (Exception e) {
            log.error("获取铁路局信息列表数据异常", e);
            query.setCode(EnumReturnResult.RETURN_404.getCode());
            query.setMsg("获取铁路局数据异常");
        }
        return query;
    }

    /**
     * 获取线路信息数据
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getLineList",method = RequestMethod.POST)
    public BaseInfoQuery getLineList(BaseInfoQuery query) {
        try {
            query.setInfoType(EnumInfoType.INFO_LINE.getCode());
            query = baseInfoService.query(query);
            query.setCode(EnumReturnResult.RETURN_200.getCode());
        } catch (Exception e) {
            log.error("获取线路信息列表数据异常", e);
            query.setCode(EnumReturnResult.RETURN_404.getCode());
            query.setMsg("获取线路数据异常");
        }
        return query;
    }

    /**
     * 获取车站信息数据
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getStationList",method = RequestMethod.POST)
    public BaseInfoQuery getStationList(BaseInfoQuery query) {
        try {
            query.setInfoType(EnumInfoType.INFO_STATION.getCode());
            query = baseInfoService.query(query);
            query.setCode(EnumReturnResult.RETURN_200.getCode());
        } catch (Exception e) {
            log.error("获取车站信息列表数据异常", e);
            query.setCode(EnumReturnResult.RETURN_404.getCode());
            query.setMsg("获取车站数据异常");
        }
        return query;
    }

    /**
     * 进入新增页面
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String preAdd(String infoType){
        if(EnumInfoType.INFO_BUREAU.getCode().equals(infoType)){
            return "baseInfo/bureau/add";
        }
        if(EnumInfoType.INFO_LINE.getCode().equals(infoType)){
            return "baseInfo/line/add";
        }
        if(EnumInfoType.INFO_STATION.getCode().equals(infoType)){
            return "baseInfo/station/add";
        }
        log.error("传入的基础信息类型有误");
        return "system/error";
    }

    /**
     * 新增基础信息
     * @param baseInfo
     * @param result
     * @return
     */
    @ResponseBody
    @OperationLog(billType ="3",actionType ="0" )
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public BaseResponse add(@Validated BaseInfo baseInfo, BindingResult result) {
        BaseResponse response = new BaseResponse();
        try {
            if(result.hasErrors()){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("BaseInfo-add参数有误");
                log.info("BaseInfo-add参数有误："+result.getAllErrors().toString());
                return response;
            }
            baseInfoService.add(baseInfo);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("新增成功");
        } catch (Exception e) {
            log.error("新增基础信息异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("新增失败");
        }
        return response;
    }

    /**
     * 修改基础信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public ModelAndView preEdit(Long id,String infoType){
        ModelAndView mav = new ModelAndView();
        try{
            BaseInfo baseInfo = baseInfoService.get(id);
            mav.addObject("baseInfo",baseInfo);
            if(EnumInfoType.INFO_BUREAU.getCode().equals(infoType)){
                mav.setViewName("baseInfo/bureau/edit");
            }
            if(EnumInfoType.INFO_LINE.getCode().equals(infoType)){
                mav.setViewName("baseInfo/line/edit");
            }
            if(EnumInfoType.INFO_STATION.getCode().equals(infoType)){
                mav.setViewName("baseInfo/station/edit");
            }
        }catch (Exception e){
            log.error("获取基础信息详情失败",e);
            mav.addObject(SystemConstant.ERROR_KEY,"系统异常");
            if(EnumInfoType.INFO_BUREAU.getCode().equals(infoType)){
                mav.setViewName("baseInfo/bureau/list");
            }
            if(EnumInfoType.INFO_LINE.getCode().equals(infoType)){
                mav.setViewName("baseInfo/line/list");
            }
            if(EnumInfoType.INFO_STATION.getCode().equals(infoType)){
                mav.setViewName("baseInfo/station/list");
            }
        }
        return mav;
    }

    /**
     * 修改基础信息
     * @param baseInfo
     * @param result
     * @return
     */
    @ResponseBody
    @OperationLog(billType ="3",actionType ="1" )
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public BaseResponse edit(@Validated BaseInfo baseInfo, BindingResult result) {
        BaseResponse response = new BaseResponse();
        try {
            if(result.hasErrors()){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("BaseInfo-edit参数有误");
                log.info("BaseInfo-edit参数有误："+result.getAllErrors().toString());
                return response;
            }
            baseInfoService.edit(baseInfo);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("修改成功");
        } catch (Exception e) {
            log.error("修改基础信息异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("修改失败");
        }
        return response;
    }

    /**
     * 删除基础信息
     * @param id
     * @return
     */
    @ResponseBody
    @OperationLog(billType ="3",actionType ="2" )
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public BaseResponse delete(Long id) {
        BaseResponse response = new BaseResponse();
        try {
            baseInfoService.delete(id);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("删除基础信息成功");
        } catch (Exception e) {
            log.error("删除基础信息异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("删除基础信息失败");
        }
        return response;
    }

    /**
     * 获取基础数据
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllList",method = RequestMethod.POST)
    public BaseInfoQuery getAllList(BaseInfoQuery query) {
        try {
            query.setData(baseInfoService.getList(query));
            query.setCode(EnumReturnResult.RETURN_200.getCode());
        } catch (Exception e) {
            log.error("获取基础数据异常", e);
            query.setCode(EnumReturnResult.RETURN_404.getCode());
            query.setMsg("获取基础数据异常");
        }
        return query;
    }

}
